package com.baeldung.bidirect;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

/**
 * Created by dzy on 2018/9/2
 */
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class UserWithIdentity {
  public int id;
  public String name;
  public List<ItemWithIdentity> userItems;

  public UserWithIdentity() {
    super();
  }

  public UserWithIdentity(final int id, final String name) {
    this.id = id;
    this.name = name;
    userItems = new ArrayList<ItemWithIdentity>();
  }

  public void addItem(final ItemWithIdentity item) {
    userItems.add(item);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder("[");
    for (ItemWithIdentity userItem : userItems) {
      sb.append(userItem.id).append(",");
    }
    if (userItems.size()>0){
      sb.deleteCharAt(sb.length() - 1);
    }
    sb.append("]");
    return "UserWithIdentity{" +
      "id=" + id +
      ", name='" + name + '\'' +
      ", userItems=" + sb.toString() +
      '}';
  }
}
